package thread;

public class Demo10 {
    private static boolean isRunning = true;
    public static void main(String[] args) {
       // boolean isRunning = true   如果在这里设置这个变量  编译会报错  这里涉及 变量捕获的问题
        Thread t = new Thread(() -> {
            while (isRunning) {
                System.out.println("hello thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
            }
            }
            System.out.println("t线程已经结束了");
        });
        t.start();

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        //3秒后  ,主线程就修改了 isRunning的值 ,从而通知t结束
        System.out.println("控制t线程结束");
        isRunning = false;
    }
}
